package com.applet.mallappletbackstage.controller;


import com.applet.mallappletbackstage.entity.Bill;
import com.applet.mallappletbackstage.service.BillService;
import com.applet.mallappletbackstage.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * (Bill)表控制层
 *
 * @author makejava
 * @since 2020-08-20 16:04:47
 */
@RestController
@RequestMapping("bill")
public class BillController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private BillService billService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param bill 查询实体
     * @return 所有数据
     */
    @GetMapping
    public R selectAll(Page<Bill> page, Bill bill) {
        return R.success(this.billService.page(page, new QueryWrapper<>(bill)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @return 单条数据
     */
    @GetMapping("one/{user}/{work}")
    public R selectOne(@PathVariable String user, @PathVariable String work) {
        QueryWrapper<Bill> billQueryWrapper = new QueryWrapper<>();
        billQueryWrapper.eq("work", work).and(billQueryWrapper1 -> billQueryWrapper1.eq("user", user));
        Bill bill = this.billService.getOne(billQueryWrapper);
        return R.success(bill);
    }

    /**
     * 新增数据
     *
     * @param bill 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R insert(@RequestBody Bill bill) {
        return R.success(this.billService.save(bill));
    }

    /**
     * 修改数据
     *
     * @param bill 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody Bill bill) {
        return R.success(this.billService.updateById(bill));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return R.success(this.billService.removeByIds(idList));
    }
}
